/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CopyUI.java
 *
 * Created on 2010.11.11., 13:10:14
 */

package workshopdesign.UI;

import workshopdesign.Control.DVDController;
import workshopdesign.Control.LoanController;
import workshopdesign.Model.Copy;
import workshopdesign.Model.DVD;
import workshopdesign.Model.Loan;
import workshopdesign.Model.Person;

/**
 *
 * @author Dom
 */
public class CopyUI extends javax.swing.JPanel implements BaseUIController {

    private CopyListUI copyListUI;
    private CopyDetailUI copyDetailUI;
    private DVDDetailUI dvdDetailUI;
    private LoanListUI loanListUI;
    private PersonDetailUI personDetailUI;

    private DVDController dvdController = DVDController.getDVDControl();
    private LoanController loanController = LoanController.getLoanController();

    private Copy selected_copy = null;
    private Loan selected_loan = null;

    /** Creates new form CopyUI */
    public CopyUI() {
        initComponents();

        copyListUI = new CopyListUI(this, "Copies");
        copyDetailUI = new CopyDetailUI(this, "Details", true);
        dvdDetailUI = new DVDDetailUI(this, "DVD Details", false);
        loanListUI = new LoanListUI(this, "Loans");
        personDetailUI = new PersonDetailUI(this, "Borrowed by", false);

        copy_list_splitter.setLeftComponent(copyListUI);
        detail_splitter.setTopComponent(copyDetailUI);
        detail_splitter.setBottomComponent(dvdDetailUI);
        loan_splitter.setTopComponent(loanListUI);
        loan_splitter.setBottomComponent(personDetailUI);

        loanListUI.setCopy(null);
    }

    @Override
    public void clear_selection() {
        copyListUI.clear();
        copyDetailUI.setCopy(null);
        dvdDetailUI.setDvd(null);
        loanListUI.setCopy(null);
        selected_copy = null;
        selected_loan = null;
    }

    @Override
    public void subselection_changed() {
        Copy copy = copyListUI.get_selected_copy();
        if(copy != selected_copy){
            selected_copy = copy;

            copyDetailUI.setCopy(copy);
            DVD dvd = dvdController.get_dvd(copy.getDVD_id());
            
            dvdDetailUI.setDvd(dvd);
            loanListUI.setCopy(copy);
        } else {
            Loan loan = loanListUI.get_selected();
            if(loan != selected_loan){
                selected_loan = loan;

                Person person = loanController.getPerson(loan.getId());
                personDetailUI.setPerson(person);
            }
        }
    }

    @Override
    public void update_data() {
        clear_selection();
        copyListUI.load_data();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        copy_list_splitter = new javax.swing.JSplitPane();
        detail_loan_splitter = new javax.swing.JSplitPane();
        detail_splitter = new javax.swing.JSplitPane();
        loan_splitter = new javax.swing.JSplitPane();

        setName("Form"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        copy_list_splitter.setDividerLocation(551);
        copy_list_splitter.setName("copy_list_splitter"); // NOI18N

        detail_loan_splitter.setDividerLocation(250);
        detail_loan_splitter.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        detail_loan_splitter.setName("detail_loan_splitter"); // NOI18N

        detail_splitter.setDividerLocation(150);
        detail_splitter.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        detail_splitter.setName("detail_splitter"); // NOI18N
        detail_loan_splitter.setTopComponent(detail_splitter);

        loan_splitter.setDividerLocation(150);
        loan_splitter.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        loan_splitter.setName("loan_splitter"); // NOI18N
        detail_loan_splitter.setRightComponent(loan_splitter);

        copy_list_splitter.setRightComponent(detail_loan_splitter);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(copy_list_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(copy_list_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        copyListUI.load_data();
        copyDetailUI.load_data();
        dvdDetailUI.load_data();
        loanListUI.load_data();
    }//GEN-LAST:event_formComponentShown


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSplitPane copy_list_splitter;
    private javax.swing.JSplitPane detail_loan_splitter;
    private javax.swing.JSplitPane detail_splitter;
    private javax.swing.JSplitPane loan_splitter;
    // End of variables declaration//GEN-END:variables

}
